package cn.yzgy.yzgyoa.common.controller;


import cn.yzgy.yzgyoa.common.service.impl.ReportRecordServiceImpl;
import cn.yzgy.yzgyoa.common.annotation.ApiDesc;
import cn.yzgy.yzgyoa.common.annotation.Syslog;
import cn.yzgy.yzgyoa.common.crsp.Urmf;
import cn.yzgy.yzgyoa.common.entity.SysFileInfo;
import cn.yzgy.yzgyoa.common.service.FileUpAndDownService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 * @author Yqsen
 * @since 2019-12-13
 */
@Slf4j
@Syslog
@RestController
@RequestMapping("/file")
public class FileDownController extends BaseController {

    private final ReportRecordServiceImpl reportRecordService;
    private final FileUpAndDownService fileUpAndDownService;
    public FileDownController(ReportRecordServiceImpl reportRecordService, FileUpAndDownService fileUpAndDownService) {
        this.reportRecordService = reportRecordService;
        this.fileUpAndDownService = fileUpAndDownService;
    }

    @ApiDesc("报表文件下载")
    @GetMapping("/fileDownload")
    @RequiresPermissions("api:common:file:fileDownload")
    public void fileDownload(String id) throws IOException {
        reportRecordService.fileDownload(id,getResponse(),getCurrentUser());
    }

    @ApiDesc("文件上传")
    @RequiresPermissions("api:common:file:fileUpload")
    @PostMapping("/upload")
    @ResponseBody
    public Urmf setFileUpload(@RequestParam(value = "file") MultipartFile file,@RequestParam("busiId") String busiId) {
        String fileId = fileUpAndDownService.uploadFile(file,busiId,getCurrentUser());
        if("".equals(fileId)){
            return Urmf.fail(getCurrentMethodName(), "文件上传失败");
        }
        return Urmf.success(getCurrentMethodName(), fileId);
    }

    @ApiDesc("获取文件路径")
    @PostMapping("/getFilePath")
    @RequiresPermissions("api:common:file:getFilePath")
    public Urmf getFilePath(@RequestBody SysFileInfo queryDto) {
        SysFileInfo sysFileInfo = fileUpAndDownService.getFilePath(queryDto);
        if(sysFileInfo!=null){
            return Urmf.success(getCurrentMethodName(), sysFileInfo);
        }
        return Urmf.fail(getCurrentMethodName(), "上传失败");
    }

    @ApiDesc("上传文件下载")
    @GetMapping("/uploadFileDownload")
    @RequiresPermissions("api:common:file:uploadFileDownload")
    public void uploadFileDownload(String id) throws IOException {
        reportRecordService.uploadFileDownload(id,getResponse());
    }

    @ApiDesc("票据预览")
    @GetMapping("/getBase64")
    @RequiresPermissions("api:common:file:getBase64")
    public Urmf getBase64(String id) {
        return Urmf.success(getCurrentMethodName(), reportRecordService.getBase64(id));
    }
}
